package com.jsnkj.basic.util;

import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.UUID;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AESUtils {
    public static String key = "qebf78XWucK/sGpNS9RM9w==";

    // 初始化向量（与服务器端相同）
    public static String vector_qubntity = "0dbeddeefe601ac142bf01ac790d7833";

    public static String encrypt(String encryptedData) {
        try {
            return encrypt(encryptedData, key, vector_qubntity);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String encrypt(String data, String key, String iv) throws Exception {
        byte[] dataBytes = data.getBytes();
        byte[] keyBytes = java.util.Base64.getDecoder().decode(key);
        byte[] ivBytes = hexStringToByteArray(iv);
        SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
        byte[] encryptedBytes = cipher.doFinal(dataBytes);
        return java.util.Base64.getEncoder().encodeToString(encryptedBytes);
    }

    public static String encrypt1(String encryptedData, String key, String iv) throws Exception {
        byte[] encryptedBytes = java.util.Base64.getDecoder().decode(encryptedData);
        byte[] keyBytes = java.util.Base64.getDecoder().decode(key);
        byte[] ivBytes = hexStringToByteArray(iv);
        SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");//算法/模式/补码方式
        IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
        return new String(decryptedBytes);
    }


    public static byte[] hexStringToByteArray(String hexString) {
        int len = hexString.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character.digit(hexString.charAt(i + 1), 16));
        }
        return data;
    }


    public static String generateRandomString(int length) {
        SecureRandom random = new SecureRandom();
        return new BigInteger(length * 5, random).toString(32);
    }

    /**
     * 32位默认长度的uuid
     * (获取32位uuid)
     *
     * @return
     */
    public static String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * (获取指定长度uuid)
     *
     * @return
     */
    public static String getUUID(int len) {
        if (0 >= len) {
            return null;
        }
        String uuid = getUUID();
        StringBuffer str = new StringBuffer();
        for (int i = 0; i < len; i++) {
            str.append(uuid.charAt(i));
        }
        return str.toString();
    }
}
